package com.simpleframe.views.components;

import java.io.Writer;

import com.simpleframe.security.AuthorityContext;
import com.simpleframe.security.User;

public class Operate extends Component {
	
	protected String url;
	protected String type;
	protected String title;
	protected User user;
	protected boolean hasAuth = true;
	
	@Override
	public boolean end(Writer writer, String tagBody) {
		evaluateParameters();
		mergeTemplate(writer,getTemplate());
		return false;
	}
	
	protected void evaluateParameters(){
		//System.out.println("url:"+url);
		String uri = url;
		if(uri.indexOf("?")!=-1){
			uri = uri.substring(0, uri.indexOf("?"));
		}
		//System.out.println("uri:"+uri);
		if(AuthorityContext.isProtectResource(uri)){
			//System.out.println("is");
			if(!AuthorityContext.verifyOperate(user.getRoleIds(), uri)){
				//System.out.println("false");
				hasAuth = false;
			}
		}
		
		addParameter("hasAuth", hasAuth);
		addParameter("url", url);
		addParameter("title", title);
		if(type!=null){
			addParameter("type",type);
		}
		
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
}
